<template>
  <div class="downApi-box">
    <div class="content-box w-card">
      <div class="title-box">
        <span>下游接口管理</span>
        <el-button style="margin-left:auto;" type="primary" icon="Plus" @click="addDownApi('add',{})">新增下游接口</el-button>
      </div>
      <div class="search-box">
        <div class="form-item">
          <span>下游名称：</span>
          <el-input v-model="searchForm.appname" clearable placeholder="请输入下游名称" @keypress.enter="searchHandle" />
        </div>
        <div class="form-item">
          <span>接口状态：</span>
          <el-select style="width:100%" v-model="searchForm.flag">
            <el-option v-for="item in flagOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
        <div class="form-item" style="width:30%;max-width:480px;">
          <span>创建时间：</span>
          <el-date-picker style="width:100%" v-model="searchForm.date" type="datetimerange" range-separator="至"
            start-placeholder="开始时间" end-placeholder="结束时间" />
        </div>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" @selection-change="handleSelectionChange" stripe>
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="appname" label="下游名称" align="center" width="140">
            <template v-slot="scope">
              {{ scope.row.appname }}
            </template>
          </el-table-column>
          <el-table-column prop="appid" label="APPID" align="center" width="150">
            <template v-slot="scope">
              {{ scope.row.appid || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="appkey" label="APPKEY" align="center" width="170">
            <template v-slot="scope">
              {{ scope.row.appkey || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="allowIps" label="IP地址" align="center">
            <template v-slot="scope">
              {{ scope.row.allowIps || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="notifyUrl" label="回调地址" align="center">
            <template v-slot="scope">
              {{ scope.row.notifyUrl || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="flag" label="接口状态" align="center">
            <template v-slot="scope">
              <div :class="'flag'+scope.row.flag">{{ scope.row.flag == 1 ? '启用' : scope.row.flag == 2 ? '禁用' : '' }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="createUser" label="开通人" align="center">
            <template v-slot="scope">
              {{ scope.row.createUser || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="开通时间" align="center" width="140">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[8] : '' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template v-slot="scope">
              <el-link type="primary" @click="addDownApi('edit',scope.row)">编辑</el-link>
              <el-link style="margin-left:10px;" v-show="scope.row.flag == 1" type="danger"
                @click="noUse(1,scope.row)">禁用</el-link>
              <el-link style="margin-left:10px;" v-show="scope.row.flag == 2" type="danger"
                @click="canUse(1,scope.row)">启用</el-link>
              <el-link style="margin-left:10px;" type="danger" @click="delateHandle(1,scope.row)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <div>
            <el-button :disabled="chooseList.length == 0" @click="noUse(2)">禁用</el-button>
            <el-button :disabled="chooseList.length == 0" @click="canUse(2)">启用</el-button>
            <el-button :disabled="chooseList.length == 0" @click="delateHandle(2)">删除</el-button>
          </div>
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <app-api ref="appApiRef" @dialogClose="getTableData" />
  <confirm-pass ref="confirmPassRef" @verification="verification" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import api from './api'
import { warningDeal, getDateTime, getWindowWidthAndHeight, successDeal } from '@/utils/utils'
import appApi from './components/appApi.vue'
import confirmPass from '@/components/confirmPass.vue'
export default {
  components: {
    appApi,
    confirmPass,
  },
  setup() {
    const appApiRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      searchForm: {
        flag: null,
      },
      tableData: [],
      handleType: null,//1 禁用；2 启用；3 删除
      handleData: '',
      chooseList: [],
      flagOptions: [
        {
          value: null,
          label: '全部',
        },
        {
          value: 1,
          label: '启用',
        },
        {
          value: 2,
          label: '禁用',
        },
      ],
    })

    function addDownApi(type, data) {
      //新增
      appApiRef.value.dialogShow(type, data)
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        startTime: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0].getTime() : null,
        endTime: (state.searchForm.date && state.searchForm.date[1]) ? state.searchForm.date[1].getTime() : null,
      }
      api.getTableData(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        flag: null,
      }
      searchHandle()
    }

    function handleSelectionChange(e) {
      state.chooseList = e
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function getappid(list) {
      const myArr = []
      list && list.map(item => {
        myArr.push(item.appid)
      })
      return myArr
    }

    //禁用
    function noUse(i, data) {
      state.handleType = 1
      if (i == 1) {
        state.handleData = data.appid
      } else {
        state.handleData = getappid(state.chooseList).join(',')
      }
      const title = '下游接口禁用提示'
      const msg = '下游接口禁用后，该下游接口不可使用。'
      confirmPassRef.value.dialogShow(title, msg)
    }

    //启用
    function canUse(i, data) {
      state.handleType = 2
      if (i == 1) {
        state.handleData = data.appid
      } else {
        state.handleData = getappid(state.chooseList).join(',')
      }
      const title = '下游接口启用提示'
      const msg = '下游接口启用后，该下游接口可使用。'
      confirmPassRef.value.dialogShow(title, msg)
    }

    //删除
    function delateHandle(i, data) {
      state.handleType = 3
      if (i == 1) {
        state.handleData = data.appid
      } else {
        state.handleData = getappid(state.chooseList).join(',')
      }
      const title = '下游接口删除提示'
      const msg = '是否确认删除下游接口？'
      confirmPassRef.value.dialogShow(title, msg)
    }

    //密码校验通过
    function verification() {
      //操作
      const obj = {
        appid: state.handleData,
        type: state.handleType,
      }
      api.apiUserState(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 300
      getTableData()
    })

    return {
      ...toRefs(state),
      appApiRef,
      confirmPassRef,
      addDownApi,
      searchHandle,
      resetHandle,
      getDateTime,
      verification,
      noUse,
      canUse,
      delateHandle,
      handleSizeChange,
      handleCurrentChange,
      handleSelectionChange,
      getTableData,
    }
  }
}
</script>

<style lang="scss" scoped>
.downApi-box {
  width: 100%;
  height: 100%;
  .content-box {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 20px;
    .title-box {
      display: flex;
      align-items: center;
      padding-bottom: 20px;
      > span {
        font-size: 16px;
        font-weight: bold;
      }
    }
    .search-box {
      display: flex;
      align-items: center;
      gap: 20px;

      .form-item {
        width: 25%;
        max-width: 300px;
        display: flex;
        align-items: center;
        gap: 5px;

        > span {
          font-size: 14px;
          flex-shrink: 0;
        }
      }
    }
    .table-box {
      margin-top: 20px;
      .table-footer {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
.flag1,
.flag2 {
  display: flex;
  align-items: center;
  gap: 5px;
  justify-content: center;
  &::before {
    content: "";
    display: block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
  }
}
.flag1 {
  color: #1266ff;
  &::before {
    background-color: #1266ff;
  }
}
.flag2 {
  color: #727780;
  &::before {
    background-color: #727780;
  }
}
</style>